/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model;

import java.lang.reflect.Constructor;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.IdFactory;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_NPCPack;
import l1j.server.server.templates.L1Npc;

// Referenced classes of package l1j.server.server.model:
// L1WarSpawn

public class L1FirewallSpawn {
	private static final Logger _log = Logger.getLogger(L1FirewallSpawn.class
			.getName());

	private static L1FirewallSpawn _instance;

	private Constructor _constructor;

	public L1FirewallSpawn() {
	}

	public static L1FirewallSpawn getInstance() {
		if (_instance == null) {
			_instance = new L1FirewallSpawn();
		}
		return _instance;
	}

	public void doSpawnFireWall(L1Character cha, int targetX, int targetY) {
		L1Npc firewall = NpcTable.getInstance().getTemplate(81157); // ファイアーウォール
		if (firewall == null) {
			throw new NullPointerException(
					"FireWall data not found:npcid=81157");
		}
		L1Character base = cha;
		for (int i = 0; i < 8; i++) {
			int a = base.targetDirection(targetX, targetY);
			int x = base.getX();
			int y = base.getY();
			if (a == 1) {
				x++;
				y--;
			} else if (a == 2) {
				x++;
			} else if (a == 3) {
				x++;
				y++;
			} else if (a == 4) {
				y++;
			} else if (a == 5) {
				x--;
				y++;
			} else if (a == 6) {
				x--;
			} else if (a == 7) {
				x--;
				y--;
			} else if (a == 8) {
				y--;
			}
			if (!base.isAttackPosition(x, y, 1)) {
				x = base.getX();
				y = base.getY();
			}
			L1NpcInstance npc = spawnFirewall(firewall, x, y, cha.getMapId());
			base = npc;
		}

	}

	private L1NpcInstance spawnFirewall(L1Npc l1npc, int locx, int locy,
			short mapid) {
		L1NpcInstance npc = null;
		try {
			String s = l1npc.getImpl();
			_constructor = Class.forName(
					"l1j.server.server.model.Instance." + s + "Instance")
					.getConstructors()[0];
			Object[] aobj = { l1npc };
			npc = (L1NpcInstance) _constructor.newInstance(aobj);
			npc.setId(IdFactory.getInstance().nextId());
			npc.setX(locx);
			npc.setY(locy);
			npc.setHomeX(locx);
			npc.setHomeY(locy);
			npc.setHeading(5);
			npc.setMap(mapid);
			L1World.getInstance().storeObject(npc);
			L1World.getInstance().addVisibleObject(npc);

			for (L1PcInstance pc : L1World.getInstance()
					.getRecognizePlayer(npc)) {
				npc.addKnownObject(pc);
				pc.addKnownObject(npc);
				pc.sendPackets(new S_NPCPack(npc));
			}
		} catch (Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
		return npc;
	}

}
